<?php
namespace app\controllers\index;

use app\models\ArticleModel;
use app\models\TagModel;
use app\models\CategoryModel;
use app\controllers\BaseController;
use app\classes\util\MulPager;


class IndexController extends BaseController
{
    public function index()
    {
        $pageSize = 10;
        $ArticleModel = new ArticleModel();
        $aRecommend = $ArticleModel->findRecommend();  // 推荐文章
        $aPosts = $ArticleModel->findPosts($pageSize, 0, ['id DESC']);    // 最新文章
        $iPostCount = $ArticleModel->getCount(['status' => ArticleModel::STATUS_SHOW]);
        $aHotPosts = $ArticleModel->findHots();

        $sPager = MulPager::page($_GET, $iPostCount, $pageSize, $_SERVER['HTTP_HOST']);
        $this->assign('posts', $aPosts);
        $this->assign('recommends', $aRecommend);
        $this->assign('hots', $aHotPosts);
        $this->assign('sPager', $sPager);
        $this->render();
    }

    /**
     * 获取文章的标签名称
     *
     * @param array $post
     * @param array $tags
     * @return array $tagNames
     */
    protected function getPostTagNames($post, $tags)
    {
        $aTagName = [];
        if (!$post['tagid']) {
            return $aTagName;
        }
        $aTagId = explode(',', trim($post['tagid'], ','));
        foreach ($aTagId as $tagId) {
            $aTagName[] = $tags[$tagId]['name'];
        }
        array_filter($aTagName);
        return $aTagName;
    }

    /**
     * 获取文章的分类名称
     *
     * @param array $post
     * @param array $cats
     * @return String  分类名称
     */
    protected function getPostCatNames($post, $cats)
    {
        $sCatName = '';
        $iCatId = $post['catid'];
        if (isset($cats[$iCatId]) && $cats[$iCatId]['name']) {
            $sCatName = $cats[$iCatId]['name'];
        }
        return $sCatName;
    }
}